// inquiry/pages/result/index.
import {
  inquitrySearh
} from '../../../utils/api'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wxImgUrl: app.globalData.wxImgUrl,
    typename: '国产保健食品',
    name: "",
    totals: 0,
    list: [],
    isEmpty: false,
    pageNum: 1,
    pageSize: 10,

    loading: false,
    scrollTop: false, //页面滚动距离
    navBarHeight: '', //头高度
    statusBarHeight: '', //状态栏高度
    tipshow: false, //底部提示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    //获取头部高度
    let navBarHeightinfo = app.publicFun.navBarHeightinfo()
    this.setData({
      navBarHeight: navBarHeightinfo[0],
      statusBarHeight: navBarHeightinfo[1]
    })

    if (options.id == 'ENTRANCE') {
      this.setData({
        typename: '进口保健食品'
      })
    }
    this.setData({
      name: options.name
    })
    this.inquitrySearh(options.name)
  },
  inquitrySearh(name) {
    wx.showLoading({
      title: '正在加载中',
      icon: 'none',
    });
    inquitrySearh({
      name: name,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }).then(res => {
      this.setData({
        list: this.data.list.concat(res.data.list),
        totals: res.data.count,
        isEmpty: res.data.list.length === 0,
        loading: true
      })
      wx.hideLoading()
    }).catch(err => {
      wx.hideLoading()
    })
  },
  back() {
    wx.navigateBack({})
  },
  handlerInfo(e) {
    let typeid = e.currentTarget.dataset.id,
      id = 0;
    if (this.data.typename == '进口保健食品') {
      id = 'ENTRANCE';
    }
    wx.navigateTo({
      url: `/inquiry/pages/info/index?id=${id}&name=${this.data.name}&typeid=${typeid}&totals=${this.data.totals}`,
    })
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.list.length >= this.data.totals) {
      this.setData({
        tipshow: true,
      })
      return
    }
    this.setData({
      pageNum: ++this.data.pageNum
    })
    this.inquitrySearh(this.data.name)
  },
  onPageScroll: function (e) {
    // 监听滚动位置控制导航
    if (e.scrollTop > 0) {
      this.setData({
        scrollTop: true
      })
    } else {
      this.setData({
        scrollTop: false
      })
    }
    // 监听滚动位置控制导航
  }
})